<?php
// +----------------------------------------------------------------------
// | 中犇单商户
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2021 中犇科技 All rights reserved.
// +----------------------------------------------------------------------

namespace addons\Getui\controller;

require_once __DIR__ . '/' . '../sdk/GTClient.php';

/**
 * V2.1版本升级
 * 2021年9月2日
 * 新的个推控制器,服务端推送接口，支持三个接口推送
 * 移除其他模板，统一使用透传模板
 * 1.PushMessageToSingle接口：支持对单个用户进行推送
 * 2.PushMessageToList接口：支持对多个用户进行推送，建议为50个用户
 * 3.pushMessageToApp接口：对单个应用下的所有用户进行推送，可根据省份，标签，机型过滤推送
 * @package addons\Getui\controller
 * @author 晓风<215628355@qq.com>
 */
class Getui
{
    // AppID
    protected $AppID;
    // AppKey
    protected $AppKey;
    // AppSecret
    protected $AppSecret;
    // masterSecret
    protected $MasterSecret;
    // 产品域名
    protected $domain = "https://restapi.getui.com";
    //开关
    protected $status;

    public function __construct()
    {      
        // 插件配置参数
        $config = addons_config('Getui');
        $this->AppID = $config['AppID'];
        $this->AppKey = $config['AppKey'];
        $this->AppSecret = $config['AppSecret'];
        $this->MasterSecret = $config['MasterSecret'];     
        $this->status = $config['status'];     
    }
    
    private function micro_time()
    {
        list($usec, $sec) = explode(" ", microtime());
        $time = ($sec . substr($usec, 2, 3));
        return $time;
    }

    /**
     * 单推
     * 晓风<215628355@qq.com>]
     * @param string $title     消息标题
     * @param string $content   消息内容
     * @param string $client_id 客户端设备ID
     * @param string $custom    透传内容
     * @param string $logo      android的LOGO设置
     * @param string $logoUrl   android的LOGO链接设置
     * @param string $sound     IOS音乐设置
     * @param int    $badge     IOS角标设置（即未读消息数量）
     * @return pushMessageToSingle
     */
    public function pushMessageToSingle($title , $content , $client_id , $custom = "" , $logo = "", $logoUrl = "",$sound = "default",$badge = -1)
    {
        if(!$this->status){
            return null;
        }
        $api = new \GTClient($this->domain, $this->AppKey, $this->AppID, $this->MasterSecret);        
        $push = $this->NotificationTemplate($title,$content,$custom,$logo,$logoUrl,$sound,$badge);
        $push->setCid($client_id);
        $res = $api->pushApi()->pushToSingleByCid($push);
        //执行推送
        return $res;     
    }


    /**
     * 多推
     * 晓风<215628355@qq.com>]
    * @param string $title     消息标题
     * @param string $content   消息内容
     * @param array   $client_id 客户端设备ID
     * @param string $custom    透传内容
     * @param string $logo      android的LOGO设置
     * @param string $logoUrl   android的LOGO链接设置
     * @param string $sound     IOS音乐设置
     * @param int    $badge     IOS角标设置（即未读消息数量）
     * @return pushMessageToSingle
     */
    function pushMessageToList($title , $content , $client_id , $custom = "" , $logo = "", $logoUrl = "",$sound = "default",$badge = -1)
    {
        if(!$this->status){
            return null;
        }
        $api = new \GTClient($this->domain, $this->AppKey, $this->AppID, $this->MasterSecret);      
        $batch = new \GTPushBatchRequest();
        foreach($client_id as $id){
            $push = $this->NotificationTemplate($title,$content,$custom,$logo,$logoUrl,$sound,$badge);
            $push->setCid($id);
            $batch->addMsgList($push);
        }   
        $batch->setIsAsync(false);        
        $res = $api->pushApi()->pushBatchByCid($batch);
        //执行推送
        return $res;
    }
   /**
     * 群推
     * 晓风<215628355@qq.com>]
     * @param string $title     消息标题
     * @param string $content   消息内容
     * @param string $custom    透传内容
     * @param string $logo      android的LOGO设置
     * @param string $logoUrl   android的LOGO链接设置
     * @param string $sound     IOS音乐设置
     * @param int    $badge     IOS角标设置（即未读消息数量）
     * @return pushMessageToSingle
     */
    public function pushMessageToApp($title , $content , $custom = "" , $logo = "", $logoUrl = "",$sound = "default",$badge = -1)
    {
        if(!$this->status){
            return null;
        }
        $api = new \GTClient($this->domain, $this->AppKey, $this->AppID, $this->MasterSecret);      
        $push = $this->NotificationTemplate($title,$content,$custom,$logo,$logoUrl,$sound,$badge);
        $push->setGroupName("test");   
        return  $api->pushApi()->pushAll($push);
    }
      /**
     * 创建一个通用模板
     * @param string $title     消息标题
     * @param string $content   消息内容
     * @param string $custom    透传内容
     * @param string $logo      android的LOGO设置
     * @param string $logoUrl   android的LOGO链接设置
     * @param string $sound     IOS音乐设置
     * @param int    $badge     IOS角标设置（即未读消息数量）
     * @return IGtTransmissionTemplate
     */
    private function NotificationTemplate($title,$content,$custom = "",$logo = "",$logoUrl = "",$sound = "default",$badge = -1){
        $push = new \GTPushRequest();
        $push->setRequestId($this->micro_time());
        //设置setting
        $set = new \GTSettings();
        $set->setTtl(3600000);
        $push->setSettings($set);

        $message = new \GTPushMessage();
        
        $notify = new \GTNotification();
        $notify->setTitle($title);
        $notify->setBody($content);
        //点击通知后续动作，目前支持以下后续动作:
        //1、intent：打开应用内特定页面url：打开网页地址。2、payload：自定义消息内容启动应用。3、payload_custom：自定义消息内容不启动应用。4、startapp：打开应用首页。5、none：纯通知，无后续动作
        $notify->setClickType("payload");
        $notify->setPayload($custom); 
        $notify->setLogo($logo);
        $notify->setLogoUrl($logoUrl);
        //$notify->setBadgeAddNum($badge);
        $message->setNotification($notify);
        //创建IOS消息模板
        
       //厂商推送消息参数
        $pushChannel = new \GTPushChannel();
        //ios
        $ios = new \GTIos();
        $ios->setType("notify");
        $ios->setAutoBadge("1");
        $ios->setPayload($custom);
        //$ios->setApnsCollapseId("apnsCollapseId");
        //aps设置
        $aps = new \GTAps();
        $aps->setContentAvailable(0);
        //$aps->setSound("com.gexin.ios.silenc");
        //$aps->setCategory("category");
        //$aps->setThreadId("threadId");

        $alert = new \GTAlert();
        $alert->setTitle($title);
        $alert->setBody($content);
        //$alert->setActionLocKey("ActionLocKey");
        //$alert->setLocKey("LocKey");
        //$alert->setLocArgs(array("LocArgs1","LocArgs2"));
        //$alert->setLaunchImage("LaunchImage");
        //$alert->setTitleLocKey("TitleLocKey");
       // $alert->setTitleLocArgs(array("TitleLocArgs1","TitleLocArgs2"));
        //$alert->setSubtitle("Subtitle");
        //$alert->setSubtitleLocKey("SubtitleLocKey");
        //$alert->setSubtitleLocArgs(array("subtitleLocArgs1","subtitleLocArgs2"));
        $aps->setAlert($alert);
        $ios->setAps($aps);

//        $multimedia = new \GTMultimedia();
//        $multimedia->setUrl("url");
//        $multimedia->setType(1);
//        $multimedia->setOnlyWifi(false);
        
//        $multimedia2 = new \GTMultimedia();
//        $multimedia2->setUrl("url2");
//        $multimedia2->setType(2);
//        $multimedia2->setOnlyWifi(true);
//        $ios->setMultimedia(array($multimedia));
//        $ios->addMultimedia($multimedia2);
        $pushChannel->setIos($ios);
        
        $push->setPushChannel($pushChannel);
        $push->setPushMessage($message);          
        return $push;
    }
    
}